﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.DAO;
using System.Web.UI.WebControls;


namespace UTS.Controls
{
    [
        DefaultProperty("Text"),
        ToolboxData("<{0}:CourseDayDropDownList runat=\"server\"> </{0}:CourseDayDropDownList>")
    ]
    public class CourseDayDropDownList : System.Web.UI.WebControls.DropDownList
    {

        private CourseDayDAO courseDayDAO = new CourseDayDAO();

        private List<CourseDay> objectList = new CourseDayDAO().getEnabledCourseDays();


        private void init()
        {
            this.DataSource = objectList;
            DropDownUtils.addDefaultLabel(this, "Select a CourseDay");
            this.Items.Add(new ListItem("Select a CourseDay",null));
            this.DataBind();
            
        }

        // this method bypasses non post back request by ajax
        public void reload()
        {
            init();
        }

        protected override void OnLoad(EventArgs e)
        {
                base.OnLoad(e);
                if (!Page.IsPostBack)
                {
                    init();
                }
        }


        private CourseDay courseDay;

        public CourseDay CourseDay
        {
            get 
            {
                if (StringUtils.isValidGuid(this.SelectedValue.ToString()))
                {
                    courseDay = courseDayDAO.findById(new Guid(this.SelectedValue.ToString()));
                }
                else
                {
                    courseDay = null;
                }

                return courseDay; 
           }
            set 
            { 
                courseDay = value;
                if (courseDay != null)
                {
                    this.SelectedValue = courseDay.CourseDayId.ToString();
                }
            }
        }
    }
}